package cn.itcast.cd.service.impl;

import java.util.List;
import java.util.Set;

import cn.itcast.cd.dao.IPermissionDao;
import cn.itcast.cd.dao.IRoleDao;
import cn.itcast.cd.dao.impl.RoleDaoImpl;
import cn.itcast.cd.domain.Permission;
import cn.itcast.cd.domain.Role;
import cn.itcast.cd.service.IPermissionService;
import cn.itcast.cd.service.IRoleService;

public class PermissionServiceImpl implements IPermissionService {

	private IPermissionDao permissionDao;
	private IRoleService roleService;
	
	
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}

	public void setPermissionDao(IPermissionDao permissionDao) {
		this.permissionDao = permissionDao;
	}

	@Override
	public void save(Permission permission) {
		permissionDao.save(permission);
	}



	@Override
	public Permission get(Long id) {
		return permissionDao.get(id);
	}



	@Override
	public void delete(Long id) {
		Permission p = permissionDao.get(id);
		List<Role> roles = roleService.queryEntity("SELECT Role", null);
		for (Role role : roles) {
		
				role.getPermissions().remove(p);
		}
		permissionDao.delete(id);
	}



	@Override
	public void update(Permission permission) {
		permissionDao.update(permission);
	}



	@Override
	public List<Permission> queryEntity(String conditions, Object[] params) {
		return permissionDao.queryEntity(conditions, params);
	}

	

}
